package com.yuda.campsite.controller;

import java.io.ByteArrayInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.oss.OSS;
import com.yuda.campsite.domain.*;
import com.yuda.campsite.service.*;
import com.yuda.campsite.service.impl.NewStudnetServiceImpl;
import com.yuda.common.core.text.UUID;
import com.yuda.common.core.utils.IdUtils;
import com.yuda.common.core.utils.SecurityUtils;
import com.yuda.common.core.utils.file.OssFileUtils;
import com.yuda.common.core.web.domain.BaseEntity;
import com.yuda.common.datascope.annotation.DataScope;
import com.yuda.common.security.service.TokenService;
import com.yuda.system.api.model.LoginUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yuda.common.log.annotation.Log;
import com.yuda.common.log.enums.BusinessType;
import com.yuda.common.security.annotation.PreAuthorize;
import com.yuda.common.core.web.controller.BaseController;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 *
 * @author wyn
 * @date 2021-09-23
 */
@RestController
@RequestMapping("/conf")
public class CampsiteConfController extends BaseController {
    @Autowired
    private NewStudnetService newStudnetService;

    @Autowired
    private ICampsiteConfService campsiteConfService;
    @Autowired
    private PeopleInfoSrevice peopleInfoSrevice;
    @Autowired
    private BaoMingStudentService baoMingStudentService;
    @Autowired
    private ShengHeStudentService shengHeStudentService;
    @Autowired
    private IStudentInfoService studentInfoService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private Client client;
    @Resource
    private ICampsiteOrderService campsiteOrderService;
    /**
     * 自动注入oss这个类
     */
    @Resource
    private OSS oss;
    /**
     * 查询【请填写功能名称】列表
     */
//    @DataScope(deptAlias = "d")
    @GetMapping("/list")
//    @PreAuthorize(hasPermi = "campSystem:campOper:list")
    public TableDataInfo list(CampsiteConf campsiteConf) {
        startPage();
        List<CampsiteConf> list = campsiteConfService.selectCampsiteConfList(campsiteConf);
        return getDataTable(list);
    }

    /**
     * 时间戳转化工具类
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }

    /**
     * 导出【请填写功能名称】列表
     */

    @Log(title = "导出营地配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CampsiteConf campsiteConf) throws IOException {
        List<CampsiteConf> list = campsiteConfService.selectCampsiteConfList(campsiteConf);
        ExcelUtil<CampsiteConf> util = new ExcelUtil<CampsiteConf>(CampsiteConf.class);
        util.exportExcel(response, list, "conf");
    }

    /**
     * 导出报名数据列表接口
     */
  //  @PreAuthorize(hasPermi = "campSystem:campManager:signup:export")
    @PostMapping("/NewExport")
    public void NewExport(HttpServletResponse response, @RequestParam(value = "campsiteId") String campsiteId) throws IOException {
        List<NewStudnet> newStudnets = newStudnetService.newStudent(campsiteId);
        /**判断该营地有没有开启审核资料这一步*/
        String s = peopleInfoSrevice.CampsiteShengHe(campsiteId);
        if (s !=null && s.equals("1")) {
            /**修改时间戳
             * 修改报名状态*/
            for (NewStudnet peopleInfo : newStudnets) {
                /**设置报名成功时间为yyyy-MM-DD HH:mm:ss*/
                peopleInfo.setPayTime(timeStamp2Date(peopleInfo.getPayTime(),null));
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("0")) {
                    peopleInfo.setStatus("进入报名流程");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("1")) {
                    peopleInfo.setStatus("已阅读报名须知");
                }
                if (peopleInfo.getStatus()!=null && peopleInfo.getStatus().equals("2") && peopleInfo.getInvitationCode()==null){
                    peopleInfo.setStatus("未使用邀请码");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("2") && peopleInfo.getInvitationCode() != null) {
                    peopleInfo.setStatus("已使用邀请码");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("3") && peopleInfo.getExamineStatus() != null && peopleInfo.getExamineStatus().equals("0")) {
                    peopleInfo.setStatus("已填写报名资料");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("4") && peopleInfo.getExamineStatus() != null && peopleInfo.getExamineStatus().equals("0")) {
                    peopleInfo.setStatus("已提交审核资料(未审核)");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("4") && peopleInfo.getExamineStatus() != null && peopleInfo.getExamineStatus().equals("1")) {
                    peopleInfo.setStatus("已提交审核资料(审核通过)");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("4") && peopleInfo.getExamineStatus() != null && peopleInfo.getExamineStatus().equals("2")) {
                    peopleInfo.setStatus("已提交审核资料(审核未通过)");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("5")) {
                    peopleInfo.setStatus("待支付");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("6")) {
                    peopleInfo.setStatus("报名完成");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("7")) {
                    peopleInfo.setStatus("已退款");
                }
                if(peopleInfo.getStatus()==null){
                    peopleInfo.setStatus("异常");
                }if (peopleInfo.getGender()!=null && peopleInfo.getGender().equals("0")){
                    peopleInfo.setGender("男");
                }if (peopleInfo.getGender()!=null && peopleInfo.getGender().equals("1")){
                    peopleInfo.setGender("女");
                }if (peopleInfo.getGender()==null || peopleInfo.getGender().equals("2")){
                    peopleInfo.setGender("未填写");
                }if (peopleInfo.getJinxiaoName()!=null  ){
                    peopleInfo.setJinxiaoName(peopleInfo.getJinxiaoName());
                }if (peopleInfo.getJinxiaoName()==null ){
                    peopleInfo.setJinxiaoName(" ");
                }if (peopleInfo.getPayWay()!=null && peopleInfo.getPayWay().equals("1")){
                    peopleInfo.setPayWay("银联");
                }if (peopleInfo.getPayWay()!=null && peopleInfo.getPayWay().equals("2")){
                    peopleInfo.setPayWay("支付宝");
                }if (peopleInfo.getPayWay()!=null && peopleInfo.getPayWay().equals("3")){
                    peopleInfo.setPayWay("微信");
                }
            }

            ExcelUtil<NewStudnet> util = new ExcelUtil<NewStudnet>(NewStudnet.class);
            util.exportExcel(response, newStudnets, "conf");
        } else {
            /**未开启审核功能的营地展示的信息*/
            for (NewStudnet peopleInfo : newStudnets) {
                peopleInfo.setPayTime(timeStamp2Date(peopleInfo.getPayTime(),null));
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("0")) {
                    peopleInfo.setStatus("进入报名流程");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("1")) {
                    peopleInfo.setStatus("已阅读报名须知");
                }
                if (peopleInfo.getStatus()!=null && peopleInfo.getStatus().equals("2") && peopleInfo.getInvitationCode()==null){
                    peopleInfo.setStatus("未使用邀请码");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("2") && peopleInfo.getInvitationCode() != null) {
                    peopleInfo.setStatus("已使用邀请码");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("3")) {
                    peopleInfo.setStatus("已填写报名资料");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("5")) {
                    peopleInfo.setStatus("待支付");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("6")) {
                    peopleInfo.setStatus("报名完成");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("7")) {
                    peopleInfo.setStatus("已退款");
                }
                if(peopleInfo.getStatus()==null){
                    peopleInfo.setStatus("异常");
                }if (peopleInfo.getGender()!=null && peopleInfo.getGender().equals("0")){
                    peopleInfo.setGender("男");
                }if (peopleInfo.getGender()!=null && peopleInfo.getGender().equals("1")){
                    peopleInfo.setGender("女");
                }if (peopleInfo.getGender()==null || peopleInfo.getGender().equals("2")){
                    peopleInfo.setGender("未填写");
                }if (peopleInfo.getJinxiaoName()!=null  ){
                    peopleInfo.setJinxiaoName(peopleInfo.getJinxiaoName());
                }if (peopleInfo.getJinxiaoName()==null ){
                    peopleInfo.setJinxiaoName(" ");
                }if (peopleInfo.getPayWay()!=null && peopleInfo.getPayWay().equals("1")){
                    peopleInfo.setPayWay("银联");
                }if (peopleInfo.getPayWay()!=null && peopleInfo.getPayWay().equals("2")){
                    peopleInfo.setPayWay("支付宝");
                }if (peopleInfo.getPayWay()!=null && peopleInfo.getPayWay().equals("3")){
                    peopleInfo.setPayWay("微信");
                }
            }
            ExcelUtil<NewStudnet> util = new ExcelUtil<NewStudnet>(NewStudnet.class);
            util.exportExcel(response, newStudnets, "conf");
        }
    }
    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(campsiteConfService.selectCampsiteConfById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "campSystem:campOper:add")
    @Log(title = "添加配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CampsiteConf campsiteConf) {
        LoginUser loginUser = tokenService.getLoginUser();
        Long deptId = loginUser.getSysUser().getDeptId();
        if (deptId != null) {
            campsiteConf.setDeptId(deptId.intValue());
        }
        List<String> list = campsiteConfService.yingdiName();
        logger.info("list" + list.size());
        if (!list.isEmpty()) {
            for (String s : list) {
                if (campsiteConf.getName().equals(s)) {
                    return AjaxResult.error("营地名称已存在，请更换一个名称");
                }
            }
        }
        String uuid = IdUtils.fastSimpleUUID();
        campsiteConf.setIdentity(uuid);
        campsiteConf.setVisitUrl("http://ying.campcenter.cn/" + uuid);
        return campsiteConfService.insertCampsiteConf(campsiteConf) > 0 ? AjaxResult.success(campsiteConf.getVisitUrl()) : AjaxResult.error("保存失败");
    }

    /**
     * 修改【请填写功能名称】
     */
    /**
     * @PreAuthorize(hasPermi = "common:conf:edit")
     */
    @PreAuthorize(hasPermi = "campSystem:campOper:edit")
    @Log(title = "修改配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CampsiteConf campsiteConf) {
        String flag = campsiteConfService.nameExists(campsiteConf);
        if (flag != null && flag.equals("1")) {
            return AjaxResult.error("营地名称已存在，请更换一个名称");
        }
       if (campsiteConf.getRegistrationEnd()!=null ){
           Date registrationEnd = campsiteConf.getRegistrationEnd();
           CampsiteOrder campsiteOrder =new CampsiteOrder();
           campsiteOrder.setEndTime(registrationEnd.getTime()/1000);
           campsiteOrder.setCampsiteId(campsiteConf.getId());
           campsiteOrderService.XuiGaiEndTime(campsiteOrder.getEndTime(),campsiteOrder.getCampsiteId());
           return toAjax(campsiteConfService.updateCampsiteConf(campsiteConf));
       }else {
           return AjaxResult.error("截止时间不能为空");
       }

    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "campSystem:campOper:remove")
    @Log(title = "批量或者单个删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids) {
        //return toAjax(campsiteConfService.deleteCampsiteConfByIds(ids));
        return toAjax(campsiteConfService.tombstone(ids));
    }

    /**
     * 系统logo文件上传的接口
     * 系统logo放在campsite的Bucket之中
     */
    @PostMapping("/uploadLogo")
    public AjaxResult uploadLogo(MultipartFile multipartFile) {
        if (multipartFile == null) {
            //return "不要上传空文件";
            return AjaxResult.error("请不要上传空文件");
        }
        if (multipartFile.getSize() >= 10485760) {
            //return "图片大小大于10MB，请上传小于10MB的图片";
            return AjaxResult.error("图片大小大于10MB，请上传小于10MB的图片");
        }
        String fileName = OssFileUtils.generateFileName(multipartFile);
        String url = null;
        String objectName = "logo/" + fileName;
        String bucketName = "campiste";
        /** 判断文件是否符合上传规范*/
        if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") || fileName.endsWith(".png")) {
            try {
                oss.putObject(bucketName, objectName, new ByteArrayInputStream(multipartFile.getBytes()));
                url = "http://campiste.oss-cn-beijing.aliyuncs.com" + "/" + objectName;
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件上传失败");
            }
        } else {
            //return "上传的文件格式不正确，请检查后重新上传";
            return AjaxResult.error("上传的文件格式不正确，请检查后上传");
        }
        return AjaxResult.success("上传成功", url);
    }

    /**
     * 系统背景图片文件上传接口
     * 系统背景图片放在campsite的Bucket之中
     */
    @PostMapping("/uploadImage")
    public AjaxResult uploadImage(MultipartFile multipartFile) {
        /**这个是multipartFile文件的判空方式，直接与Null进行比较*/
        if (multipartFile == null) {
            //return "不要上传空文件";
            return AjaxResult.error("不要上传空文件");
        }
        if (multipartFile.getSize() >= 10485760) {
            //return "图片大小大于10MB，请上传小于10MB的图片";
            return AjaxResult.error("图片大小大于10MB，请上传小于10MB的图片");
        }
        String fileName = OssFileUtils.generateFileName(multipartFile);
        String url = null;
        String objectName = "image/" + fileName;
        String bucketName = "campiste";
        /** 判断文件是否符合上传的规范*/

        if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") || fileName.endsWith(".png")) {
            try {
                oss.putObject(bucketName, objectName, new ByteArrayInputStream(multipartFile.getBytes()));
                url = "http://campiste.oss-cn-beijing.aliyuncs.com" + "/" + objectName;
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件上传失败");
            }
        } else {
            //return "上传的文件格式不正确，请检查后重新上传";
            return AjaxResult.error("上传的文件格式不正确，请检查后重新上传");
        }
        return AjaxResult.success("请求成功", url);
    }

    /**
     * 上传pdf文件接口
     */
    @PostMapping("/uploadPdf")
    public AjaxResult uploadPdf(MultipartFile multipartFile) {
        if (multipartFile == null) {
            return AjaxResult.error("不能上传空文件");
        }
        String fileName = OssFileUtils.generateFileName(multipartFile);
        String url = null;
        String objectName = "pdf/" + fileName;
        String bucketName = "campiste";
        /** 判断文件上传是否为指定文件类型*/
        if (fileName.endsWith(".pdf")) {
            try {
                oss.putObject(bucketName, objectName, new ByteArrayInputStream(multipartFile.getBytes()));
                url = "http://campiste.oss-cn-beijing.aliyuncs.com" + "/" + objectName;
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件上传失败");
            }
        } else {
            return AjaxResult.error("请上传PDF文件");
        }
        return AjaxResult.success("上传成功", url);
    }

    /**
     * 通过状态和名称查询营地
     */
    @DataScope(deptAlias = "d")
    @GetMapping("/SecondList")
    public AjaxResult SecondList(BaseEntity baseEntity,
                                 @RequestParam(value = "onOff", required = false) String onOff,
                                 @RequestParam(value = "name", required = false) String name) {
        List<CampsiteConf> campsiteConf = campsiteConfService.xuanZhe(baseEntity.getParams(), onOff, name);
        return AjaxResult.success("请求成功", campsiteConf);
    }

    /**
     * 开启营地
     */
    @Log(title = "营地操作(开启关闭)", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermi = "campSystem:campOper:status")
    @PostMapping("/kaiqi")
    public AjaxResult kaiqi(String onOff, String name) {
        campsiteConfService.kaiqi(onOff, name);
        return AjaxResult.success("操作成功");
    }

    /**
     * 关闭营地
     */
    @Log(title = "关闭营地", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermi = "campSystem:campOper:status")
    @PostMapping("/guanbi")
    public AjaxResult guanbi(String onOff,String name) {
        campsiteConfService.guanbi(onOff, name);
        return AjaxResult.success("操作成功");
    }
    /* *//** 进入审核接口带分页*//*
    @GetMapping("/shenghe")
    public AjaxResult shenghe(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
    *//* String a = String.valueOf(((page-1) *pageSzie));
     String b = String.valueOf(pageSzie);*//*
        Integer a =((page-1)*pageSize);
        Integer b =pageSize;
        List<BaoMingStudent> baoMingStudents = baoMingStudentService.ALLStudent(a, b);
        return AjaxResult.success("查询成功",baoMingStudents);
    }*/

    /**
     * 审核单个学生接口
     */
    @GetMapping("/dangeshenghe")
    public AjaxResult dangeshenghe(@RequestParam("phone") String phone, @RequestParam("campsiteId") String campsiteId) {
        BaoMingStudent baoMingStudent = baoMingStudentService.DanGeStudent(phone, campsiteId);
        baoMingStudent.setJiguanProvince(studentInfoService.selectProId(baoMingStudent.getJiguanProvince()));
        baoMingStudent.setJiguanCity(studentInfoService.selectCityId(baoMingStudent.getJiguanCity()));
        baoMingStudent.setSchoolProvince(studentInfoService.selectSchProId(baoMingStudent.getSchool()));
        baoMingStudent.setSchoolCity(studentInfoService.selectSchCityID(baoMingStudent.getSchool()));
        return AjaxResult.success("请求成功", baoMingStudent);
    }

    /**
     * 审核通过接口
     */
    @Log(title = "审核通过", businessType = BusinessType.UPDATE)
    @PostMapping("/shenghe")
    public AjaxResult shenghetongguo(@RequestParam("examineStatus") String examineStatus, @RequestParam("idCard") String idCard, @Param("campsiteId") String campsiteId) {
        int shenghesuccess = baoMingStudentService.shenghesuccess(examineStatus, idCard, campsiteId);
        /**审核之后发送短信*/
        String s = campsiteConfService.SelectPhone(idCard);
        String s1 = campsiteConfService.SelectName(idCard);
        String s2 = campsiteConfService.SelectYingDiName(campsiteId);
        if (s!=null){
            SendSmsRequest sendSmsRequest = new SendSmsRequest();
            sendSmsRequest.setSignName("CAMPCENTER");
            sendSmsRequest.setPhoneNumbers(s);
            sendSmsRequest.setTemplateCode("SMS_229638807");
            JSONObject finalJsonParameters = new JSONObject();
            finalJsonParameters.put("name",s1);
            finalJsonParameters.put("camp_name",s2);
            sendSmsRequest.setTemplateParam(finalJsonParameters.toJSONString());
            try {
                SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return AjaxResult.success("审核通过成功");
    }
    //* 审核不通过原因接口
  /*  @GetMapping("/failreason")
    public  AjaxResult failreason(@RequestParam("examineReason") String examineReason,@RequestParam("idCard") String idCard){
        int i = baoMingStudentService.FailReason(examineReason, idCard);
        return AjaxResult.success("请求成功");
    }*/

    /**
     * 审核查询带分页接口
     */

    @GetMapping("/shenghechaxun")
    public AjaxResult shenghechaxun(@RequestParam(value = "jiguanProvince", required = false) String jiguanProvince,
                                    @RequestParam(value = "jiguanCity", required = false) String jiguanCity,
                                    @RequestParam(value = "name", required = false) String name,
                                    @RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "offset", defaultValue = "10") Integer offset) {
        List<BaoMingStudent> shenghechaxun = baoMingStudentService.shenghechaxun(jiguanProvince, jiguanCity, name, page, offset);
        Integer a = shenghechaxun.size();
        List<PageResult> pageResults = new ArrayList<>();
        for (BaoMingStudent baoMingStudent : shenghechaxun) {
            PageResult pageResult = new PageResult();
            pageResult.setTotal(String.valueOf(a));
            pageResult.setData(baoMingStudent);
            pageResults.add(pageResult);
        }
        return AjaxResult.success("查询成功", pageResults);
    }

    /**
     * 查看报名的接口
     */
    @GetMapping("/chaXun")
    @ResponseBody
 //   @PreAuthorize(hasPermi = "campSystem:campManager:signup")
    public String chaXun2(@RequestParam(value = "campsiteId", required = true) String campsiteId,
                          @RequestParam(value = "school",required = false) String school,
                          @RequestParam(value = "jiguanProvince", required = false) String jiguanProvince,
                          @RequestParam(value = "jiguanCity", required = false) String jiguanCity,
                          @RequestParam(value = "status", required = false) String status,
                          @RequestParam(value = "examineStatus",required = false) String examineStatus,
                          @RequestParam(value = "invitationCode",required = false) String invitationCode,
                          @RequestParam(value = "phone", required = false) String phone,
                          @RequestParam(value = "idCard", required = false) String idCard,
                          @RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Integer a = (page - 1) * pageSize;
        Integer b = pageSize;
        JSONObject jsonObject = new JSONObject();
        /**修改total的大小*/
        List<PeopleInfo> peopleInfoList = peopleInfoSrevice.chaXun(campsiteId, school, jiguanProvince, jiguanCity, status, examineStatus, invitationCode, phone, idCard,  0, 100000);
        List<PeopleInfo> peopleInfos = peopleInfoSrevice.chaXun(campsiteId, school, jiguanProvince, jiguanCity, status, examineStatus, invitationCode, phone, idCard, a, b);
        int size = peopleInfoList.size();
        jsonObject.put("total", size);
        jsonObject.put("message", "请求成功");
        /**判断该营地有没有开启审核资料这一步*/
        String s = peopleInfoSrevice.CampsiteShengHe(campsiteId);
        if (s.equals("1")) {
            /**修改时间戳
             * 修改报名状态*/
            for (PeopleInfo peopleInfo : peopleInfos) {
                peopleInfo.setPayTime((timeStamp2Date(peopleInfo.getPayTime(), null)));
                peopleInfo.setAddTime(timeStamp2Date(peopleInfo.getAddTime(), null));
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("0")) {
                    peopleInfo.setQianDuanStatus("进入报名流程");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("1")) {
                    peopleInfo.setQianDuanStatus("已阅读报名须知");
                }
                if (peopleInfo.getStatus()!=null && peopleInfo.getStatus().equals("2") && peopleInfo.getInvitationCode()==null){
                    peopleInfo.setQianDuanStatus("未使用邀请码");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("2") && peopleInfo.getInvitationCode() != null) {
                    peopleInfo.setQianDuanStatus("已使用邀请码");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("3") && peopleInfo.getExamineStatus() != null && peopleInfo.getExamineStatus().equals("0")) {
                    peopleInfo.setQianDuanStatus("已填写报名资料");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("4") && peopleInfo.getExamineStatus() != null && peopleInfo.getExamineStatus().equals("0")) {
                    peopleInfo.setQianDuanStatus("已提交审核材料(未审核)");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("4") && peopleInfo.getExamineStatus() != null && peopleInfo.getExamineStatus().equals("1")) {
                    peopleInfo.setQianDuanStatus("已提交审核资料(审核通过)");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("4") && peopleInfo.getExamineStatus() != null && peopleInfo.getExamineStatus().equals("2")) {
                    peopleInfo.setQianDuanStatus("已提交审核资料(审核未通过)");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("5")) {
                    peopleInfo.setQianDuanStatus("待支付");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("6")) {
                    peopleInfo.setQianDuanStatus("报名完成");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("7")) {
                    peopleInfo.setQianDuanStatus("已退款");
                }
            }
        }else {
            /**未开启审核功能的营地展示的信息*/
            for (PeopleInfo peopleInfo : peopleInfos) {
                peopleInfo.setPayTime((timeStamp2Date(peopleInfo.getPayTime(), null)));
                peopleInfo.setAddTime(timeStamp2Date(peopleInfo.getAddTime(), null));
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("0")) {
                    peopleInfo.setQianDuanStatus("进入报名流程");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("1")) {
                    peopleInfo.setQianDuanStatus("已阅读报名须知");
                }
                if (peopleInfo.getStatus()!=null && peopleInfo.getStatus().equals("2") && peopleInfo.getInvitationCode()==null){
                    peopleInfo.setQianDuanStatus("未使用邀请码");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("2") && peopleInfo.getInvitationCode() != null) {
                    peopleInfo.setQianDuanStatus("已使用邀请码");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("3")) {
                    peopleInfo.setQianDuanStatus("已填写报名资料");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("5")) {
                    peopleInfo.setQianDuanStatus("待支付");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("6")) {
                    peopleInfo.setQianDuanStatus("报名完成");
                }
                if (peopleInfo.getStatus() != null && peopleInfo.getStatus().equals("7")) {
                    peopleInfo.setQianDuanStatus("已退款");
                }
            }
        }
        jsonObject.put("code", 200);
        jsonObject.put("data", peopleInfos);

        return jsonObject.toJSONString();
    }

    /**
     * 待审核学生接口
     */
    @GetMapping("/daishenghe")
    public String daishenghe(@RequestParam(value = "campsiteId") String campsiteId,
                             @RequestParam(value = "jiguanProvince", required = false) String jiguanProvince,
                             @RequestParam(value = "jiguanCity", required = false) String jiguanCity,
                             @RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        JSONObject jsonObject = new JSONObject();
        Integer a = (page - 1) * pageSize;
        Integer b = pageSize;
        List<ShengHeStudnet> shenghetiaojian1 = shengHeStudentService.shenghetiaojian(campsiteId, jiguanProvince, jiguanCity, name, 0, 10000);
        List<ShengHeStudnet> shenghetiaojian = shengHeStudentService.shenghetiaojian(campsiteId, jiguanProvince, jiguanCity, name, a, b);
        for (ShengHeStudnet shengHeStudnet : shenghetiaojian) {
            shengHeStudnet.setSubmitTime(timeStamp2Date(shengHeStudnet.getSubmitTime(), "yyyy-MM-dd HH:mm:ss"));
            if (shengHeStudnet.getStatus() != null && shengHeStudnet.getStatus().equals("0")) {
                shengHeStudnet.setStatus("进入报名流程");
            }
            if (shengHeStudnet.getStatus() != null && shengHeStudnet.getStatus().equals("1")) {
                shengHeStudnet.setStatus("已阅读报名须知");
            }
            if (shengHeStudnet.getStatus()!=null && shengHeStudnet.getStatus().equals("2") && shengHeStudnet.getInvitationCode()==null){
                shengHeStudnet.setStatus("未使用邀请码");
            }
            if (shengHeStudnet.getStatus() != null && shengHeStudnet.getStatus().equals("2") && shengHeStudnet.getInvitationCode() != null) {
                shengHeStudnet.setStatus("已使用邀请码");
            }
            if (shengHeStudnet.getStatus() != null && shengHeStudnet.getStatus().equals("3") ) {
                shengHeStudnet.setStatus("已填写报名资料");
            }
            if (shengHeStudnet.getStatus() != null && shengHeStudnet.getStatus().equals("4") ) {
                shengHeStudnet.setStatus("已提交审核材料(未审核)");
            }

        }
        int size = shenghetiaojian1.size();
        jsonObject.put("code", 200);
        jsonObject.put("message", "查询成功");
        jsonObject.put("data", shenghetiaojian);
        jsonObject.put("total", size);
        return jsonObject.toJSONString();
    }

    /**
     * 查询审核通过的学生接口
     */
    @GetMapping("/shenghetongguochaxun")
    public String shenghetongguochaxun(@RequestParam(value = "campsiteId") String campsiteId,
                                       @RequestParam(value = "jiguanProvince", required = false) String jiguanProvince,
                                       @RequestParam(value = "jiguanCity", required = false) String jiguanCity,
                                       @RequestParam(value = "name", required = false) String name,
                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        JSONObject jsonObject = new JSONObject();
        Integer a = (page - 1) * pageSize;
        Integer b = pageSize;
        List<ShengHeStudnet> shenghetongguo = shengHeStudentService.shenghetongguo(campsiteId, jiguanProvince, jiguanCity, name, 0, 10000);
        List<ShengHeStudnet> shenghetiaojian = shengHeStudentService.shenghetongguo(campsiteId, jiguanProvince, jiguanCity, name, a, b);
        for (ShengHeStudnet shengHeStudnet : shenghetiaojian) {
            shengHeStudnet.setSubmitTime(timeStamp2Date(shengHeStudnet.getSubmitTime(), "yyyy-MM-dd HH:mm:ss"));
        }
        int size = shenghetongguo.size();
        jsonObject.put("code", 200);
        jsonObject.put("message", "查询成功");
        jsonObject.put("data", shenghetiaojian);
        jsonObject.put("total", size);
        return jsonObject.toJSONString();
    }

    /**
     * 查询审核不通过学生接口
     */
    @GetMapping("/shenghebutongguo")
    public String shenghebutongguo(@RequestParam(value = "campsiteId") String campsiteId,
                                   @RequestParam(value = "jiguanProvince", required = false) String jiguanProvince,
                                   @RequestParam(value = "jiguanCity", required = false) String jiguanCity,
                                   @RequestParam(value = "name", required = false) String name,
                                   @RequestParam(value = "page", defaultValue = "1") Integer page,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        JSONObject jsonObject = new JSONObject();
        Integer a = (page - 1) * pageSize;
        Integer b = pageSize;
        List<ShengHeStudnet> shenghebutongguo = shengHeStudentService.shenghebutongguo(campsiteId, jiguanProvince, jiguanCity, name, 0, 10000);
        List<ShengHeStudnet> shenghetiaojian = shengHeStudentService.shenghebutongguo(campsiteId, jiguanProvince, jiguanCity, name, a, b);
        for (ShengHeStudnet shengHeStudnet : shenghetiaojian) {
            shengHeStudnet.setSubmitTime(timeStamp2Date(shengHeStudnet.getSubmitTime(), "yyyy-MM-dd HH:mm:ss"));
        }
        int size = shenghebutongguo.size();
        jsonObject.put("code", 200);
        jsonObject.put("message", "查询成功");
        jsonObject.put("data", shenghetiaojian);
        jsonObject.put("total", size);
        return jsonObject.toJSONString();
    }

    /**
     * 查看报名人数
     */
    @GetMapping("/chakanbaoming")
    public AjaxResult chakanbaoming(@RequestParam("name") String name) {
        Integer chakanbaoming = shengHeStudentService.chakanbaoming(name);
        return AjaxResult.success("报名人数为:", chakanbaoming);
    }

    /**
     * 审核不通过接口
     */
    @Log(title = "审核不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/failshenghe")
    public AjaxResult failshenghe(@RequestParam(value = "examineReason", required = false) String examineReason,
                                  @RequestParam("idCard") String idCard,
                                  @RequestParam("campsiteId") String campsiteId) {
        if (examineReason == null || examineReason.equals("")) {
            return AjaxResult.error("申请不通过原因必须写");
        }
        shengHeStudentService.failshenghe(examineReason, idCard, campsiteId);
        /**审核不通过之后发送短信*/
        String s = campsiteConfService.SelectPhone(idCard);
        /**查出学生姓名*/
        String s1 = campsiteConfService.SelectName(idCard);
        /**查出学生所在营地的名字*/
        String s2 = campsiteConfService.SelectYingDiName(campsiteId);
        /**如果手机号不为null则发短信*/
        if (s!=null) {
            SendSmsRequest sendSmsRequest = new SendSmsRequest();
            sendSmsRequest.setSignName("CAMPCENTER");
            sendSmsRequest.setPhoneNumbers(s);
            sendSmsRequest.setTemplateCode("SMS_230270032");
            JSONObject finalJsonParameters = new JSONObject();
            finalJsonParameters.put("name", s1);
            finalJsonParameters.put("camp_name", s2);
            sendSmsRequest.setTemplateParam(finalJsonParameters.toJSONString());
            try {
                SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return AjaxResult.success("操作成功");
    }
}


